﻿using Avalonia.Data.Converters;
using Avalonia.Media;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SD.App.Desktop.Converters;
/// <summary>
/// 错误信息 → 可见性（非空则显示，空则隐藏）
/// </summary>
public class ErrorToVisibleConverter : IValueConverter
{
    /// <summary>
    /// 完全匹配 IValueConverter 接口签名（参数均为可空）
    /// </summary>
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo? culture)
    {
        // 安全处理可空值，避免空引用
        var error = value as string ?? string.Empty;
        return string.IsNullOrEmpty(error) ? false : true;
    }

    /// <summary>
    /// 反向转换无需实现
    /// </summary>
    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo? culture)
    {
        throw new NotImplementedException("ErrorToVisibleConverter 不支持反向转换");
    }
}

/// <summary>
/// 错误信息 → 边框色（有错误为红色，无错误为灰色）
/// </summary>
public class ErrorToBorderBrushConverter : IValueConverter
{
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo? culture)
    {
        var error = value as string ?? string.Empty;
        var hasError = !string.IsNullOrEmpty(error);

        // 返回 SolidColorBrush（Avalonia 可直接识别）
        return hasError
            ? "Error" // 错误红色
            : "Default"; // 默认灰色
    }

    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo? culture)
    {
        throw new NotImplementedException("ErrorToBorderBrushConverter 不支持反向转换");
    }
}

/// <summary>
/// 错误信息 → 文本颜色（有错误为红色，无错误为灰色）
/// </summary>
public class ErrorToForegroundConverter : IValueConverter
{
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo? culture)
    {
        var error = value as string ?? string.Empty;
        var hasError = !string.IsNullOrEmpty(error);

        return hasError
            ? new SolidColorBrush(Color.Parse("#EF4444")) // 错误红色
            : new SolidColorBrush(Color.Parse("#6B7280")); // 默认灰色
    }

    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo? culture)
    {
        throw new NotImplementedException("ErrorToForegroundConverter 不支持反向转换");
    }
}


